package AgentTravel.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Collection;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import AgentTravel.LogicObjects.Language;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

public class LanguageDialog extends JDialog {

	private BindingGroup m_bindingGroup;
	private JPanel m_contentPane;
	private AgentTravel.LogicObjects.Language language;
	private JComboBox nameJComboBox;
	private List<String> m_languages;
	private JButton btnOk;

	/**
	 * Create the dialog.
	 */
	public LanguageDialog(List<String> languages, Language lang) {
		setTitle("AddLanguage");
		m_languages = languages; 
		language = lang;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);		
		
		setBounds(100, 100, 450, 300);
		m_contentPane = new JPanel();
		setContentPane(m_contentPane);
		//
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0E-4 };
		m_contentPane.setLayout(gridBagLayout);

		JLabel nameLabel = new JLabel("Name:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		m_contentPane.add(nameLabel, labelGbc_0);

		nameJComboBox = new JComboBox();
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		m_contentPane.add(nameJComboBox, componentGbc_0);
		
		btnOk = new JButton("ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		GridBagConstraints gbc_btnOk = new GridBagConstraints();
		gbc_btnOk.anchor = GridBagConstraints.EAST;
		gbc_btnOk.gridx = 1;
		gbc_btnOk.gridy = 2;
		m_contentPane.add(btnOk, gbc_btnOk);

		if (language != null) {
			m_bindingGroup = initDataBindings();
		}
	}

	public AgentTravel.LogicObjects.Language getLanguage() {
		return language;
	}

	public void setLanguage(AgentTravel.LogicObjects.Language newLanguage) {
		setLanguage(newLanguage, true);
	}

	public void setLanguage(AgentTravel.LogicObjects.Language newLanguage,
			boolean update) {
		language = newLanguage;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (language != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}
	protected BindingGroup initDataBindings() {
		BeanProperty<Language, String> languageBeanProperty = BeanProperty.create("name");
		BeanProperty<JComboBox, Object> jComboBoxBeanProperty = BeanProperty.create("selectedItem");
		AutoBinding<Language, String, JComboBox, Object> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, language, languageBeanProperty, nameJComboBox, jComboBoxBeanProperty);
		autoBinding.bind();
		//
		JComboBoxBinding<String, List<String>, JComboBox> jComboBinding = SwingBindings.createJComboBoxBinding(UpdateStrategy.READ, m_languages, nameJComboBox);
		jComboBinding.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		//
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(jComboBinding);
		return bindingGroup;
	}
}
